﻿using HandyControl.Data;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace 三维转台控制系统
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private static Mutex? mutex;
        public App()
        {
            this.Startup += new StartupEventHandler(App_Startup);
            DispatcherUnhandledException += App_DispatcherUnhandledException;
        }

        //兜底未处理异常（对于无await的异步中的异常无法捕获）
        private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {

            //HandyControl.Controls.MessageBox.Show($"{e.Exception.Message}",
            //        "异常",
            //        MessageBoxButton.OK,
            //        MessageBoxImage.Warning, MessageBoxResult.OK);
            HandyControl.Controls.Growl.Warning(e.Exception.Message);
            e.Handled = true;
        }

        private void App_Startup(object sender, StartupEventArgs e)
        {
            //单例模式
            mutex = new Mutex(true, $"{AppDomain.CurrentDomain.FriendlyName}", out bool ret);
            if (!ret)
            {
                //if (HandyControl.Controls.MessageBox.Show($"{AppDomain.CurrentDomain.FriendlyName}不能重复打开!",
                //    "提示",
                //    MessageBoxButton.OK,
                //    MessageBoxImage.Warning) == MessageBoxResult.OK)
                //{
                //    System.Environment.Exit(0);
                //}



                if (HandyControl.Controls.MessageBox.Show(
                new HandyControl.Data.MessageBoxInfo()
                {
                    Message = $"{AppDomain.CurrentDomain.FriendlyName}不能重复打开!",
                    Caption = "提示",
                    Button = MessageBoxButton.OK,
                    IconBrushKey = ResourceToken.AccentBrush,
                    IconKey = ResourceToken.ErrorGeometry,
                    StyleKey = "MessageBoxCustom1"
                }
                ) == MessageBoxResult.OK)
                {
                    System.Environment.Exit(0);
                }
            }
        }
    }
}
